﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Entities;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.BIZ;

namespace Assessment.Management.UI.Admin.UserControls.Teachers
{
    public partial class TeacherEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdID.Value = GetParam();
                int id = int.Parse(GetParam());
                LoadDataByID(id);
                DepartmentParam param = new DepartmentParam(FunctionType.GetListDepartment);
                MainController.Provider.Execute(param);
                ddlKhoa.DataSource = param.listDepartment;
                ddlKhoa.DataBind();
                ddlKhoa.Items.Insert(0, new ListItem("----------Chọn Khoa----------", ""));
                ddlKhoa.SelectedIndex = 0;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UpdateTeacher();
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=teacherdisplay&id=" + hdID.Value);
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {

            Response.Redirect("UniAdmin.aspx?do=teachermanager");
        }
      
        
        private void UpdateTeacher()
        {
            TeacherParam param = new TeacherParam(FunctionType.UpdateTeacher);
            Teacher teacher = new Teacher();
            teacher.ID = int.Parse(txtMaGV.Text);
            teacher.TeacherName = txtTenGV.Text.Trim();
            teacher.DepartmentID = int.Parse(ddlKhoa.SelectedValue);
            teacher.Address = txtAdress.Text.Trim();
            teacher.Mobile = txtMobile.Text.Trim();
            teacher.Email = txtEmail.Text.Trim();
            teacher.Version = int.Parse(hdVersion.Value);
            teacher.UpdatedBy = SMX.smx_UpdatedBy;
            teacher.UpdatedDTG = DateTime.Now;
            param.Teacher = teacher;
            MainController.Provider.Execute(param);
            
        }

        protected string GetParam()
        {
            return Request.Params["id"];
        }
        //Lấy ra sản phẩm để sửa. Đầu vào là mã sản phẩm
        private void LoadDataByID(int id)
        {
            TeacherParam param = GetTeacherToEditByID(id);
            BindItemToForm(param);
        }
        //Load dữ liệu lên form
        private void BindItemToForm(TeacherParam param)
        {
            txtMaGV.Text = GetParam();
            txtTenGV.Text = param.Teacher.TeacherName;
            txtAdress.Text = param.Teacher.Address;
            txtMobile.Text = param.Teacher.Mobile;
            txtEmail.Text = param.Teacher.Email;
            hdVersion.Value = param.Teacher.Version.ToString();
        }
        //Lấy ra sản phẩm để sửa. Đầu vào là mã sản phẩm
        private TeacherParam GetTeacherToEditByID(int id)
        {
            TeacherParam param = new TeacherParam(FunctionType.GetTeacherToEditByCode);
            param.Teacher = new Teacher() { ID = id };
            MainController.Provider.Execute(param);
            return param;
        }

        protected void ddlKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}